<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\reservation\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\reservation\model\reservation;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 预约列表逻辑层
 */
class reservationLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new reservation();
    }
    public function getReservationList($userId,$limit){
        return $this->model
        ->rightJoin('eb_space', 'eb_reservation.space_id = eb_space.space_id')
        ->field('eb_space.space_id,eb_space.space_title,eb_space.space_image,eb_space.rent_nature,eb_space.rent_price,eb_space.building_area,eb_space.address,
        eb_space.project_id,eb_reservation.create_time,eb_reservation.status
        ,eb_reservation.reject_reason,eb_reservation.reservation_time,eb_reservation.reservation_time_slot')
        ->where('eb_reservation.user_id', $userId)
        ->paginate($limit);
    }
    public function export($where = [])
    {
        $query = $this->search($where);
        // ->field('id,name,enterprise_id,sort,status,create_time');
        $data = $this->getAll($query);
   

       
        $file_name = '预约数据.xlsx';
         $header = ['预约项目', '预约时段', '预约人', '身份证号', '手机号', '预约人数', '预约时间', '审核状态'];
    
        $filter = [
            // 'activity_status' => [
            //     ['value' => 0, 'label' => '进行中'],
            //     ['value' => 1, 'label' => '已结束'],
            //     ['value' => 2, 'label' => '未开始']
            // ]
        ];
    
        $data_attr = [];
        foreach($data as $value){
        $data_attr[] = [
            'project_id_text' => $value['project_id_text'],
            'reservation_time_slot' => $value['reservation_time_slot'],
            'reservation_person' => $value['reservation_person'],
            'id_number' => $value['id_number'],
            'phone_number' => $value['phone_number'],
            'reservation_number' => $value['reservation_number'],
            'reservation_time_text' => $value['reservation_time_text'],
            'status_text' => $value['status_text']
        ];
        }

        $writer = new OpenSpoutWriter($file_name);
        $writer->setWidth([20, 20, 20, 20, 20, 20,20,20]);
        $writer->setHeader($header);
        $writer->setData($data_attr, null, $filter);
        $file_path = $writer->returnFile();
        return response()->download($file_path, urlencode($file_name));
    }


}
